﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using DatabaseComponent;

public partial class TestCut : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Persons objPersons = GlobalVars.getCurrentPerson();
        RegisteredCoursesDB objRegDB = new RegisteredCoursesDB(ConnectionStrings.LMS);
        
        GlobalVars.sendLogMail(objRegDB.getRegisteredCoursesByID(61)[0], objPersons, 1);

        
        
        //if (!Page.IsPostBack)
        //{
        //    Page.DataBind();


        //    testDiv.Style["background"] = "url('" + getBackGround() + "') no-repeat;";

           
            
        //}

        //ClientScript.RegisterStartupScript(this.GetType(), "PosCalc", "calculatePos();", true);

        //if (int.Parse(hiddenLeft.Value) != 0)
        //{
        //    drawImage();
        //}

    }

    private void drawImage()
    {
        if (Request["image"] != null)
        {
            try
            {
                System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("~/LmsWebSite/TestImages/" + Request["image"]));
                int nCutLeft = int.Parse(hiddenLeft.Value);
                int nCutTop = int.Parse(hiddenTop.Value);
                int nCutHeight = int.Parse(hiddenHeight.Value);
                int nCutWidth = int.Parse(hiddenWidth.Value);


                img =  getNewImage(img,500, 750);



                float nXPer = (float)img.Width / 500;
                float nYPer = (float)img.Height / 750;


                nCutLeft = (int)(nCutLeft * nXPer);
                nCutWidth = (int)(nCutWidth * nXPer);

                nCutTop = (int)(nCutTop * nYPer);
                nCutHeight = (int)(nCutHeight * nYPer);



                Bitmap bmNew = new Bitmap(nCutWidth, nCutHeight);
                bmNew.SetResolution(img.HorizontalResolution, img.VerticalResolution);
                Graphics grNew = Graphics.FromImage(bmNew);

                grNew.DrawImage(img, new Rectangle(0, 0, nCutWidth, nCutHeight), new Rectangle(nCutLeft, nCutTop, nCutWidth, nCutHeight), GraphicsUnit.Pixel);




                grNew.Dispose();

                string strImageName = System.Guid.NewGuid().ToString() + ".jpg";

                bmNew.Save(Server.MapPath("~/LmsWebSite/CuttedImages/" + strImageName));

                imgCut.ImageUrl = "getImage.ashx?Image=CuttedImages/" + strImageName + "&w=500&h=750&color=white";



            }
            catch
            {
            }

        }


        hiddenLeft.Value = "0";
        hiddenTop.Value = "0";
        hiddenHeight.Value = "0";
        hiddenWidth.Value = "0";
    }

    private System.Drawing.Image getNewImage(System.Drawing.Image img ,int newWidth, int newHeight)
    {
        int newnewWidth = newWidth;
        int newnewHeight = newnewWidth * img.Height/img.Width;
        if (newnewHeight > newHeight)
        {
            newnewHeight = newHeight;
            newnewWidth = newnewHeight * img.Width / img.Height;
        }


        int newImageWidth = 0;
        int newImageHeight = 0;

        if (newnewWidth == newWidth)
        {
            float xWidthPer = (float)img.Width / newnewWidth;
            int subHeight = (int)(newHeight * xWidthPer);


             newImageWidth = img.Width;
             newImageHeight = subHeight;

            



        }
        else
        {
            float xHeightPer = (float)img.Height / newnewHeight;

            int subWidth = (int)(newWidth * xHeightPer);


             newImageHeight = img.Height;
             newImageWidth = subWidth;
        }

        Bitmap bmNew = new Bitmap(newImageWidth, newImageHeight);
        bmNew.SetResolution(img.HorizontalResolution, img.VerticalResolution);
        Graphics grNew = Graphics.FromImage(bmNew);

        grNew.DrawImage(img, new Point((bmNew.Width - img.Width) / 2, (bmNew.Height - img.Height) / 2));


        grNew.Dispose();

        return (System.Drawing.Image)bmNew;

    }


    protected string getBackGround()
    {
        if (Request["image"] != null)
            return "getimage.ashx?Image=TestImages/" + Request["image"] + "&w=500&h=750&color=white";
        else
            return "none";

    }


    protected string getID()
    {
        return testDiv.ClientID;
    }

}
